<template>
  <div id="login-page">
    <div class="login">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
        v-if="formBlooen"
      >
        <el-form-item>
          <h2>用户登录</h2>
        </el-form-item>

        <el-form-item prop="username">
          <el-input
            class="int"
            prefix-icon="el-icon-user"
            placeholder="请输入账号"
            type="text"
            v-model="ruleForm.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            class="int"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item prop="phoneNumber">
          <el-input
            class="int"
            prefix-icon="el-icon-phone-outline"
            placeholder="请输入手机号"
            type="text"
            v-model.number="ruleForm.phoneNumber"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            class="int code-int"
            placeholder="请输入验证码"
            type="password"
            v-model.number="ruleForm.code"
            autocomplete="off"
          ></el-input>
          <el-button
            type="primary"
            class="btn-code"
            :disabled="disabled"
            @click="setCode('ruleForm')"
            >{{ this.btnText }}</el-button
          >
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            @click="submitForm('ruleForm')"
            class="login-btn"
            >登录 <span class="el-icon-right"></span
          ></el-button>
        </el-form-item>
      </el-form>

      <div class="right-btn">
        <span class="right-title">魔方医院后台管理系统</span>
      </div>
    </div>
  </div>
</template>
<script>
import { mapActions } from "vuex";
import { login } from "../../utils/api/login";
import storage from "../../utils/storage";
export default {
  name: "loginPage",
  components: {},
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    var validatephone = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入手机号"));
      } else {
        callback();
      }
    };
    var validatecode = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入验证码"));
      } else {
        callback();
      }
    };
    return {
      radio: "账号登录",
      ruleForm: {
        username: "",
        password: "",
        phoneNumber: "",
        code: "",
      },

      rules: {
        username: [{ validator: validatePass, trigger: "blur" }],
        password: [{ validator: validatePass2, trigger: "blur" }],
        phoneNumber: [{ validator: validatephone, trigger: "blur" }],
        code: [{ validator: validatecode, trigger: "blur" }],
      },
      time: 60,
      disabled: false,
      btnText: "验证码",
      formBlooen: "true",
    };
  },
  methods: {
    ...mapActions("user", ["setUser"]),
    submitForm(formName) {
      let user = {};
      user.username = this.ruleForm.username;
      user.password = this.ruleForm.password;
      if (this.ruleForm.username != "" && this.ruleForm.password != "") {
        login(user).then((data) => {
          console.log(data);
          if (data.data.code == 200) {
            storage.setStorage("username", user.username);
            // storage.setStorage("token", data.data.data.token);
            this.$router.push("/home");
          }
        });
      }
      login();
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(1111);
          // this.setUser({ ...this.ruleForm }).then((data) => {
          //   console.log(data);
          //   this.$router.push("/home");
          // });
        } else {
          console.log("error submit!!");

          return false;
        }
      });
    },

    setCode() {
      this.$refs.ruleForm.validateField("phoneNumber", (errorMessage) => {
        if (!errorMessage) {
          let timer = setInterval(() => {
            this.time--;
            this.btnText = `${this.time}s`;
            this.disabled = true;
            if (this.time === 0) {
              this.disabled = false;
              this.btnText = "重新发送";
              this.time = 60;
              clearInterval(timer);
            }
          }, 1000);
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
#login-page {
  width: 100%;
  height: 100%;
  background-color: #333;
}
.login {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  box-shadow: 0 6px 8px 8px rgba(0, 0, 0, 0.2);
  border-radius: 16px;
  overflow: hidden;
}
.right-btn {
  width: 360px;
  background-color: rgba(0, 0, 0, 0);
  display: flex;
  flex-flow: column;
  color: #fff;
  align-items: center;
  justify-content: center;
  .right-title {
    padding: 12px 24px;
    border: 1px solid #fff;
    border-image: linear-gradient(
        to bottom right,
        rgb(255, 255, 255),
        rgba(0, 0, 0, 0),
        rgb(255, 255, 255)
      )
      10;
  }
}
.el-form {
  padding: 40px;
  width: 280px;
  background-color: #fff;
}
.el-form-item {
  margin-bottom: 28px !important;
}
.int /deep/ .el-input__inner {
  border: none !important;
  background-color: #f1f1f1 !important;
  &::placeholder {
    color: #999 !important;
  }
}
.code-int {
  width: 50%;
}
.login-btn {
  float: right;
  padding: 12px 48px;
}

.btn-code {
  float: right;
  width: 40%;
  text-align: center;
}
</style>
